/*
 * Klasa zarządzająca wyświetlaniem gry. Obiekty do wyświetlenia
 * rejestrowane są w kontenerze (metoda add).
 * Metoda render() iteruje po tym kontenerze i wyświetla kafelki.
 */
#include "DisplayManager.h"
#include <cstdio>
DisplayManager *DisplayManager::instance = NULL;

DisplayManager *DisplayManager::getInstance()
{
	if (instance == NULL)
	{
		instance = new DisplayManager();
	}

	return instance;
}

DisplayManager::DisplayManager()
{
}

void DisplayManager::init(int w, int h)
{
	windowWidth = w;
	windowHeight = h;
	buffer = create_bitmap(windowWidth, windowHeight);
}

DisplayManager::~DisplayManager()
{
	destroy_bitmap(buffer);
}

void DisplayManager::add(ATile *tile)
{
	tilesContainer.push_back(tile);
}

void DisplayManager::render()
{
	vector<ATile *>::iterator it;
	BITMAP *img;
	clear_to_color(buffer, makecol(10,10,10));
	for (it = tilesContainer.begin(); it < tilesContainer.end(); it++)
	{
		img = (*it)->getImage();
		
		rotate_sprite(buffer, img, (*it)->x, (*it)->y, itofix(getRotation((*it)->direction)));
//		masked_blit(img, buffer, 0, 0, (*it)->x, (*it)->y, img->w, img->h);
	}

	blit(buffer, screen, 0,0, 0, 0, windowWidth, windowHeight);
}

int DisplayManager::getRotation(int dir)
{
	switch (dir)
	{
		case Direction::NORTH:
			return 0;
		case Direction::EAST:
			return 64;
		case Direction::SOUTH:
			return 128;
		case Direction::WEST:
			return 128+64;
		default:
			return Direction::NORTH;
	}
}
